from typing import List

from app.models.battery import Battery
from tortoise.exceptions import DoesNotExist

async def create_battery(data: dict) -> Battery:
    """
    创建一个新的电池记录。
    :param data: 电池字段字典
    :return: 创建的 Battery 实例
    """
    return await Battery.create(**data)

async def get_battery_by_id(battery_id: int) -> Battery:
    """
    根据 battery_id 查询单个电池记录。
    :param battery_id: 电池主键ID
    :return: Battery 实例
    """
    return await Battery.get(battery_id=battery_id)

async def list_batteries() -> list[Battery]:
    """
    查询所有电池记录。
    :return: Battery 实例列表
    """
    return await Battery.all()

async def delete_battery(battery_id: int) -> int:
    """
    根据 battery_id 删除电池记录。
    :param battery_id: 电池主键ID
    :return: 删除的记录数（0或1）
    """
    return await Battery.filter(battery_id=battery_id).delete()


async def delete_batteries(battery_ids: List[int]) -> int:
    deleted = await Battery.filter(battery_id__in=battery_ids).delete()
    return deleted  # 返回删除条数